import api from '@/api/trace'
import { Message } from 'element-ui';
import Vue from 'vue';
const trace = {
    state: {
        myFootMark: [],
        myFootMarkFilter: []
    },

    mutations: {
        saveTrace: (state, traceList) => {
            state.myFootMark.splice(0,state.myFootMark.length);
            const time = [];
            //整理出总共有几天的数据 保存一个日期数组
            traceList.forEach(item => {
                if(!time.includes(item.trace_time)){
                    time.push(item.trace_time);
                }
            });
            //整理myFootMark的数组对象
            state.myFootMark = time.map(t => {
                return {
                    date: t,
                    goods: []
                }
            });
            //根据给到的数据的日期去往myFootMark.goods插值
            traceList.forEach(item => {
                state.myFootMark.forEach( myFoot => {
                    if(myFoot.date === item.trace_time){
                        myFoot.goods.push(item);
                    }
                });
            });
        },
        searchTraceDate:(state, date) => {
            state.myFootMarkFilter.splice(0,state.myFootMarkFilter.length);
            state.myFootMark.forEach(item => {
                if(item.date === date){
                    state.myFootMarkFilter.push(item);
                }
            });
        }
    },

    actions: {
        async fetchGetTrace({ commit },payload) {
            const { code, extend: { result } } = await api.getTrace(payload);
            if(code === 200){
                commit('saveTrace', result.traceList)
            }
        },
        async fetchDeleteOneTrace({ _ },payload) {
            const { code } = await api.deleteOneTrace(payload);
            if(code === 200){
                Message({
                    type: 'success',
                    message: '已删除。',
                    showClose: true
                });
            }else{
                Message({
                    type: 'error',
                    message: '删除失败,请重试。',
                    showClose: true
                });
            }
        },
        async fetchDeleteTraceByTime({ _ },payload) {
            const { code } = await api.deleteTraceByTime(payload);
            if(code === 200){
                Message({
                    type: 'success',
                    message: '已删除。',
                    showClose: true
                });
            }else{
                Message({
                    type: 'error',
                    message: '删除失败,请重试。',
                    showClose: true
                });
            }
        },
    }
}

export default trace